package com.zeng.incast.videoplay;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.zeng.incast.videoplay.filemanager.PreviewActivity;
import com.zeng.incast.videoplay.live.LiveActivity;


public class MainActivity extends AppCompatActivity {
    private String TAG = "videoplay";

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO};
    private static int REQUEST_PERMISSION_CODE = 1;

    private Button mLiveButton;
    private Button mPreviewButton;
    private Button mDownloadButton;
    private Button mUploadButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
                return;
            }
        }

        activityWork();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        activityWork();
    }

    private void activityWork() {
        setContentView(R.layout.main);

        initView();
    }
    
    private void initView() {
        mLiveButton = findViewById(R.id.live);
        mPreviewButton = findViewById(R.id.file_preview);
        mDownloadButton = findViewById(R.id.file_download);
        mUploadButton = findViewById(R.id.file_upload);
        ButtonOnClickListener onClickListener = new ButtonOnClickListener();
        mLiveButton.setOnClickListener(onClickListener);
        mPreviewButton.setOnClickListener(onClickListener);
        mDownloadButton.setOnClickListener(onClickListener);
        mUploadButton.setOnClickListener(onClickListener);
    }

    class ButtonOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.live:
                    Log.i(TAG, "onClick: live\n");
                    deal(LiveActivity.class);
                    break;
                case R.id.file_preview:
                    Log.i(TAG, "onClick: Preview\n");
                    deal(PreviewActivity.class);
                    break;
                case R.id.file_download:
                    Log.i(TAG, "onClick: download\n");
                    break;
                case R.id.file_upload:
                    Log.i(TAG, "onClick: upload\n");
                    break;
                default:
                    Log.e(TAG, "onClick: default\n");
                    break;
            }
        }
    }

    private void deal(Class c) {
        Intent intent = new Intent(this, c);
        startActivity(intent);
    }
}
